package com.dkcd.chenshiproject.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dkcd.chenshiproject.MainActivity;
import com.dkcd.chenshiproject.R;
import com.dkcd.chenshiproject.entity.Song;
import com.dkcd.chenshiproject.tabHostActivity.MusicPavilionActivity;
import com.dkcd.chenshiproject.util.MusicUtils;

import java.util.List;

/**
 * com.dkcd.musicplayerdemo.util 新建一个类
 *
 * @author ChenShi
 * @version：1.0
 * @date 2022/5/26
 */
public class MusicAdapter extends BaseAdapter {
    private Context context;
    private List<Song> list;
    private int defaultSelection = -1;

    public MusicAdapter(Activity mainActivity, List<Song> list) {
        this.context = mainActivity;
        this.list = list;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Log.e("TAG", "getView: getView" );
        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            //引入布局
            view = View.inflate(context, R.layout.music_pavilion_list_item, null);
            //实例化对象
            holder.song = (TextView) view.findViewById(R.id.item_mymusic_song);
            holder.singer = (TextView) view.findViewById(R.id.item_mymusic_singer);
            holder.duration = (TextView) view.findViewById(R.id.item_mymusic_duration);
            holder.position = (TextView) view.findViewById(R.id.item_mymusic_postion);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        //给控件赋值
        holder.song.setText(list.get(i).song.toString());
        holder.singer.setText(list.get(i).singer.toString());
        //时间需要转换一下
        int duration = list.get(i).duration;
        String time = MusicUtils.formatTime(duration);
        holder.duration.setText(time);
        holder.position.setText(i+1+"");

        if (i == defaultSelection){
            holder.song.setTextColor(Color.parseColor("#00FA9A"));
            holder.singer.setTextColor(Color.parseColor("#00FA9A"));
            holder.duration.setTextColor(Color.parseColor("#00FA9A"));
            holder.position.setTextColor(Color.parseColor("#00FA9A"));
        }else {
            holder.song.setTextColor(Color.BLACK);
            holder.singer.setTextColor(Color.BLACK);
            holder.duration.setTextColor(Color.BLACK);
            holder.position.setTextColor(Color.BLACK);
        }

        return view;
    }

    /**
     * @param position
     *            设置高亮状态的item
     */
    public void setSelectPosition(int position) {
        if (!(position < 0 || position > list.size())) {
            defaultSelection = position;
            notifyDataSetChanged();
        }
    }

    class ViewHolder{
        TextView song;//歌曲名
        TextView singer;//歌手
        TextView duration;//时长
        TextView position;//序号

    }

}